package vn.mobiistar.videoapp.activity;

import vn.mobiistar.videoapp.R;
import vn.mobiistar.videoapp.fragment.MenuLeftFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.LayoutParams;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnOpenedListener;

public class BaseMenuActivity extends BaseActivity {

	public final static String ARG_ACCOUNT_TYPE = "ACCOUNT_TYPE";
	public final static String ARG_AUTH_TYPE = "AUTH_TYPE";
	public final static String ARG_ACCOUNT_NAME = "ACCOUNT_NAME";
	public final static String ARG_IS_ADDING_NEW_ACCOUNT = "IS_ADDING_ACCOUNT";

	public static final String KEY_ERROR_MESSAGE = "ERR_MSG";

	public final static String PARAM_USER_PASS = "USER_PASS";

	private final int REQ_SIGNUP = 1;
	TextView navTitle ;
	ImageView navSearch;
	public Bundle savedInstanceState;
	public ImageView navSearchButton;
	public ImageView navSearchRemoveButton;
	public EditText navSearchText;

	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		this.savedInstanceState = savedInstanceState;
		super.onCreate(savedInstanceState);

		if (savedInstanceState == null) {

		} else {

		}

		initActionbar(); 
		initSlideMenu();
	    initUI();
		
	}
	public void initActionbar(){
		ActionBar actionBar = getSupportActionBar();
		actionBar.setCustomView(getLayoutInflater().inflate(R.layout.navigation_bar,null), new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
	}
	public void initUI(){
		if(getSupportActionBar() != null && getSupportActionBar().getCustomView() != null){
			this.navTitle 				= (TextView)getSupportActionBar().getCustomView().findViewById(R.id.nav_title);
			this.navSearchButton 		= (ImageView) getSupportActionBar().getCustomView().findViewById(R.id.nav_search_button);
			this.navSearchRemoveButton 	= (ImageView) getSupportActionBar().getCustomView().findViewById(R.id.nav_remove);
			this.navSearchText = (EditText) getSupportActionBar().getCustomView().findViewById(R.id.nav_textsearch);
			if(this.navTitle != null && getTitle() != null){
				this.navTitle.setText(getTitle());
			}
		}
	}
	
	public void setTitle(String title){
		if(this.navTitle != null){
			this.navTitle.setText(title);
		}
	}

	public void initSlideMenu(){
		SlidingMenu menu = getSlidingMenu();
		setBehindContentView(R.layout.sliding_menu_left_frame);
		//menu.setSecondaryMenu(R.layout.sliding_menu_right_frame);
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.menu_left_framelayout, new MenuLeftFragment());
		transaction.commit();
		menu.setBehindOffset(convertDipToPixels(50.0F));
		menu.setMode(SlidingMenu.LEFT);
		menu.setOnOpenedListener(new OnOpenedListener() {
			@Override
			public void onOpened() {
				BaseMenuActivity.this.forceHideSoftKeyboard();
			}
		});

		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
	}

	public int convertDipToPixels(float size)
	{
		return (int)(0.5F + size * getResources().getDisplayMetrics().density);
	}

	public void onClick_LeftSlideMenu(View v){
		showMenu();
	}

	public void onClick_RighSlideMenu(View v){
		showSecondaryMenu();
	}

	public void onClick_GoHomePage(View paramView)
	{
	}
	public void onClick_GoCategoryListPage(View paramView)
	{

	}
	public void onClick_GoVideoListPage(View paramView)
	{
	}
	public void onClick_NewVideoListPage(View paramView)
	{
	}
	public void onClick_MostviewVideoListPage(View paramView)
	{
	}

	
	
	
	
	public void onClick_BackFromMenuLeftSettings(View paramView)
	{
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
		transaction.replace(R.id.menu_left_framelayout, Fragment.instantiate(this, new MenuLeftFragment().getClass().getName())).commit();
	}


	
	public void onClick_ShareApp(View v){
		Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
		sharingIntent.setType("text/plain");
		String shareBody = "Here is the share content body";
		sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
		sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
		startActivity(Intent.createChooser(sharingIntent, "Share via"));
	}
	
	
	private void forceHideSoftKeyboard(){
		try{
			InputMethodManager input = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE);
			if ((getCurrentFocus() != null) && (input != null))
				input.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 2);
		}catch(Exception e){

		}
	}
}
